Crate rusqlite

source ·
Expand description

Rusqlite is an ergonomic wrapper for using SQLite from Rust.

Historically, the API was based on the one from rust-postgres. However, the two have diverged in many ways, and no compatibility between the two is intended.

use rusqlite::{params, Connection, Result};

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute(
        "CREATE TABLE person (
            id   INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            data BLOB
        )",
        (), // empty list of parameters.
    )?;
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        data: None,
    };
    conn.execute(
        "INSERT INTO person (name, data) VALUES (?1, ?2)",
        (&me.name, &me.data),
    )?;

    let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
    let person_iter = stmt.query_map([], |row| {
        Ok(Person {
            id: row.get(0)?,
            name: row.get(1)?,
            data: row.get(2)?,
        })
    })?;

    for person in person_iter {
        println!("Found person {:?}", person.unwrap());
    }
    Ok(())
}

Re-exports§

Modules§

  • backupbackup
    Online SQLite backup API.
  • blobblob
    Incremental BLOB I/O.
  • Configure database connections
  • functionsfunctions
    Create or redefine SQL functions.
  • hookshooks
    Commit, Data Change and Rollback Notification Callbacks
  • limitslimits
    Run-Time Limits
  • tracetrace
    Tracing and profiling functions. Error and warning log.
  • Traits dealing with SQLite data types.
  • vtabvtab
    Create virtual tables.

Macros§

  • A macro making it more convenient to pass lists of named parameters as a &[(&str, &dyn ToSql)].
  • A macro making it more convenient to longer lists of parameters as a &[&dyn ToSql].
  • prepare_and_bindrusqlite-macros
    Captured identifiers in SQL
  • Captured identifiers in SQL

Structs§

Enums§

Constants§

Traits§

Functions§

  • rusqlite’s check for a safe SQLite threading mode requires SQLite 3.7.0 or later. If you are running against a SQLite older than that, rusqlite attempts to ensure safety by performing configuration and initialization of SQLite itself the first time you attempt to open a connection. By default, rusqlite panics if that initialization fails, since that could mean SQLite has been initialized in single-thread mode.
  • Constructor function for a ParamsFromIter. See its documentation for more.
  • Transform Rust error to SQLite error (message and code).
  • Returns the SQLite version as a string; e.g., "3.16.2" for version 3.16.2.
  • Returns the SQLite version as an integer; e.g., 3016002 for version 3.16.2.

Type Aliases§

  • A typedef of the result returned by many methods.